## "China's Ideological Spectrum"
## by Jennifer Pan and Yiqing Xu

## install.packages("maptools")
## install.packages("plyr")
## install.packages("foreign")
## install.packages("xtable")
## install.packages("Hmisc")

########################################
## Figure 2. Describle frequencies (map)
########################################

## prepare border and provincial shape
library(maptools)
rivers<-readShapePoly("maps/hyd2_4p.shp")
CNborder<-readShapePoly("maps/bou1_4p.shp")
CNborder<-CNborder[which(CNborder$AREA>0.01),]
provshape<-readShapePoly("maps/bou2_4p.shp")
provshape$gb<-provshape$ADCODE93/10000
provshape$col<-"gray90"
provshape$col[provshape$gb==71]<-"white"
provshape<-provshape[which(provshape$AREA>0.01 & provshape$BOU2_4M_ID<80),] 

## load data
library(foreign)
library(plyr)
d<-read.dta("data/zuobiao_raw_frame.dta")

## draw circles on provincial capitals
jingwei<-read.csv("maps/prov_capital.csv",head=T)[,c("provgb","longitude","latitude")]
jingwei<-rbind(jingwei,c(99,90,22)) # abroad

counts<-c(by(d,d$provgb,function(x) sum(x$ipwgt)))
prov<-c("Beijing","Tianjin","Hebei","Shanxi","Inner Mongolia",
        "Liaoning","Jilin","Heilongjiang","Shanghai","Jiangsu",
        "Zhejiang","Anhui","Fujian","Jiangxi","Shandong","Henan",
        "Hubei","Hunan","Guangdong","Guangxi","Hainan","Chongqing",
        "Sichuan","Guizhou","Yunnan","Tibet","Shaanxi","Gansu",
        "Qinghai","Ningxia","Xinjiang","Hong Kong","Abroad")
count.prov<-cbind.data.frame(as.numeric(names(counts)),
                             prov,as.numeric(round(counts)))
colnames(count.prov)<-c("provgb","prov","freq")
circle<-join(count.prov, jingwei, by='provgb', type='left', match='all')

## plot
png("graphs/map_freq.png",width=1000,height=500)
par(mar=c(0,0,0,0))
plot(provshape,col=provshape$col);plot(CNborder,add=TRUE)
plot(rivers,col="deepskyblue",border="deepskyblue",add=T,lwd=0.2)
symbols(circle[,c(4,5)],circles=sqrt(circle$freq),
        fg="red",bg="#FFFF6680",inches=0.55,lwd = 2, add=TRUE)
text(90,22,"Abroad\n45.1K")
graphics.off()

#########################################
## Figure 3. Age and Gender Distributions
#########################################

rm(list=ls(all=TRUE))
load("data/sample10K.RData")
names(d)

library(foreign)
d0 <- read.dta("data/zuobiao_wgt_frame.dta")
names(d0)

pdf("graphs/stat_hist.pdf",width=14,height=7)
par(mfcol = c(1, 2), mar = c(4, 5, 2, 1))
## original
barplot(table(d0$female, d0$age)/dim(d0)[1],
        ylim = c(0, 0.105),
        main="", xlab="",ylab="Density", 
        col=c("gray87","gray48"), 
        legend = c("Male","Female"),
        args.legend = list(x = "topright", bty = "n", cex=1.5),
        names.arg=18:60,
        space = c(0.8),
        cex.axis=1.2,
        cex.lab=1.5,
        cex.names=1.2)
mtext("Age", side = 1, line = 2.5, cex=1.5); box();
## reweighted
barplot(table(d$female, d$age)/dim(d)[1],
        ylim = c(0,0.105),
        main="", xlab="", ylab="Density", 
        col=c("gray87","gray48"), 
        legend = c("Male","Female"),
        args.legend = list(x = "topright", bty = "n", cex=1.5), 
        names.arg=18:60, space=1,
        cex.axis=1.2,
        cex.lab=1.5,
        cex.names=1.2);
mtext("Age", side = 1, line = 2.5, cex=1.5); box();
graphics.off()

####################################
## Table A.1 Descriptive Statistics
####################################

rm(list=ls(all=TRUE))
load("data/sample10K.RData")
qcontent <- c(
    "People should not have universal suffrage if they have not been educated about democracy.",
    "Universality of human rights take precedence over sovereignty.",
    "When events that have major repercussions for the safety and security of people occur, the government should freely disseminate information even if information disclosure increases the risks of unrest.",
    "Western multiparty systems are unsuitable for China in its current state.",
    "Indiscriminately imitating (systems of) western-style freedom of speech will lead to social disorder in China.",
    "It is preferable to let universities recruit students by themselves than to have a unified national college entrance examination system.",
    "Religoius adherents should be allowed to conduct missionary  work in nonreligious spaces.",
    "Primary school, secondary school, and college students should all participate in government organized military training.",
    "National unity and territorial integrity are the highest interest of society.",
    "Even if procedural rules are violated in the process of investigation and evidence gathering, those who have actually committed crimes should be punished.",
    "The state has an obligation to provide foreign aid.",
    "It is acceptable desmirch the images of national leaders and founding leaders in literary and artistic works.",
    "When laws fail to fully constrain criminal behaviors, people have the right to impose their own punishments for these behaviors.",
    "Media should be allowed to represent the voice of a particular social stratum or interest group.",
    "If it has sufficient state capabilities, China has the right to take any action to defend its national interests.",
    "Force should be used to reunfy Taiwan with China if conditions permit.",
    "Lawyers should do their utmost to defend clients even if the client has committed a crime.",
    "Chinese citizens should be allowed to hold foreign citizenship.",
    "It is impossible for western countries led by the United States to tolerate the rise of China into a major power.",
    "The state should take measures to train and support athletes so they can win glory for the country in various international competitions.",
    "The minimum wage should be set by the state.",
    "The fruits of China's economic development since reform and opening up are enjoyed by a small group of people; most people have not received much benefit.",
    "In the decision-making of major (infrastructure) projects, individual interests should give way to social interests.",
    "Wasting food is an individual freedom.",
    "If the price of pork is too high, the government should intervene.",
    "A high tariff should be imposed on imported goods that are also produced domestically to protect domestic industries.",
    "Education should be public to the greatest extent.",
    "The interests of state-owned enterprises are part of the national interest.",
    "Attempting to control real estate prices will undermine economic development.",
    "The primary means to improve the lives of the low-income people is to give them fiscal subsidies and support.",
    "A rich person deserves better medical services.",
    "High income earners should disclose the sources of their income.",
    "People who make money through gains from financial investments contribute less to the society than people make money through labor.",
    "It is better to sell state-owned enterprises to capitalists than to let them go bankrupt.",
    "Sectors related to national security and important to the national economy and people's livelihoods must be controlled by state-owned enterprises.",
    "The process of capital accumulation is always accompanied by harm to the working class.",
    "Individuals should be able to own, buy and sell land.",
    "The government should adopt higher grain purchasing prices to boost the income of peasants.",
    "Foreign capital in China should enjoy the same treatment as national capital.",
    "Natural monopolies that emerge out of market competitions are harmless.",
    "Two adults should be free to engage in voluntary sexual behavior regardless of their marital status.",
    "One should not openly comment on the shortcomings of their elders.",
    "The modern Chinese society needs Confucianism.",
    "The fundamental standard to evaluate the value of a work of art is whether it is liked by the masses.",
    "Even with population pressures, the state and the society have no right to interfere in the decision to have a child, or how many children to have.",
    "The Eight Diagrams (Bagua) in The Book of Changes (Zhouyi) can explain many things well.",
    "The perspective of traditional Chinese medicine on human health is superior to that of modern mainstream medical science.",
    "It is unnecessary to push forward the simplification of Chinese characters.",
    "Traditional Chinese classics should be the basic education material for children.",
    "I will recognize the relationship between my child and a same-sex partner if it is a voluntary choice." 
)


questions <- paste("q", 1:50, sep = "")
stat.mean <- apply(d[, questions], 2, mean)
stat.sd <- apply(d[, questions], 2, sd)
out <- cbind.data.frame(1:50, qcontent,  stat.mean, stat.sd, t(apply(d[, questions], 2, table))/dim(d)[1])
colnames(out) <- c("No","Question","Mean","SD","(1)","(2)","(3)","(4)")
out
library(xtable)
print(xtable(out, digits = c(0, 0, 0, rep(2, 6))),
      include.rownames = FALSE, file = "output/zuobiao_stat.txt")


####################################
## Table A.2 Regional Distributoin
####################################


## read data
rm(list=ls(all=TRUE))
library(foreign)
library(plyr)
d0 <- read.dta("data/zuobiao_raw_frame.dta")

prov <- c("Beijing","Tianjin","Hebei","Shanxi","Inner Mongolia",
          "Liaoning","Jilin","Heilongjiang","Shanghai","Jiangsu",
          "Zhejiang","Anhui","Fujian","Jiangxi","Shandong","Henan",
          "Hubei"," Hunan","Guangdong","Guangxi","Hainan","Chongqing",
          "Sichuan","Guizhou","Yunnan"," Xizang","Shaanxi","Gansu",
          "Qinghai","Ningxia","Xinjiang","Hong Kong","Overseas")

provgb <- as.factor(sort(unique(d0$provgb)))
count <- c(by(d0, d0$provgb, function(x) sum(x$ipwgt)))
percent <- count/sum(count)*100
out <- cbind.data.frame(prov, provgb, count, percent)
colnames(out) <- c("prov","provgb","count_old","percent_old")

load("data/sample10K.RData")
count <- table(d$provgb)
percent <- c(count)/dim(d)[1]*100
out2 <- cbind.data.frame(count, percent)
colnames(out2) <- c("provgb","count_new","percent_new")


output <- join(out, out2, by = "provgb", type = "full")
output <- rbind.data.frame(output, c(NA,NA,apply(output[,3:6], 2, sum, na.rm = TRUE)))
output <- cbind.data.frame(output[,c(1,3,4)],"n.a." = rep(NA,dim(output)[1]), output[,5:6])

library(xtable)
print(xtable(output, digits = c(0, 0, 0, 2,0, 0,2)),
      include.rownames = FALSE, file = "output/zuobiao_prov.txt")
